from rest_framework.generics import ListAPIView,CreateAPIView
from users.models import User
from . import user_serializer
from meiduo_mall_admin.my_paginate import MyPageNumberPagination
from rest_framework.mixins import CreateModelMixin

class UserView(ListAPIView,CreateAPIView):
    pagination_class = MyPageNumberPagination
    serializer_class = user_serializer.UserSerializer
    # queryset = User.objects.order_by("id").all()

    #1,重写数据源
    def get_queryset(self):
        #1,获取查询关键字
        keyword = self.request.query_params.get("keyword")

        #2,判断是否有关键字
        if keyword:
            return User.objects.filter(username__contains=keyword).order_by("id").all()
        else:
            return User.objects.order_by("id").all()



